package cn.wangkai.peanut.db.util;

import java.io.Serializable;
import java.util.List;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

/**
 * 分页组件
 * @author Administrator
 * @param <T>
 * @param <T>
 *
 */
public class PageModel<T> implements Serializable {
	private static final long serialVersionUID = 6790992480217587929L;
	private List<T> lists;
	//每页数
	private Long pagerow;
	//当前页数
	private Long pagenum;
	//总记录条数
	private Long rowcount;

	private long pagecount;

	public PageModel() {
	}

	/**
	 * 获得数据集合
	 * @return
	 */


	public List<T> getLists() {
		return lists;
	}

	public void setLists(List<T> list) {
		this.lists = list;
	}
	
	public JSONObject toJSON(){
		JSONObject json = new JSONObject();
		try {
			JSONObject fy = new JSONObject();
			fy.put("total", new Long(this.getRowcount()));
			fy.put("pageno", new Long(this.getPagenum()));
			fy.put("pagesize", new Long(this.getPagerow()));
			fy.put("totalpage", new Long(this.getPagecount()));
			json.put("list", this.getLists());
			json.put("page", fy);
		} catch (JSONException e) { }
		return json;
	}
	
	public String toJSONString(){
		return toJSON().toJSONString();
	}

	/**
	 * 每页记录条数
	 * @return
	 */
	public Long getPagerow() {
		return pagerow;
	}

	public void setPagerow(Long pagerow) {
		this.pagerow = pagerow;
	}
	public void setPagerow(Integer pagerow) {
		this.pagerow = pagerow.longValue();
	}
	/**
	 * 当前页数
	 * @return
	 */
	public Long getPagenum() {
		if(pagenum>getPagecount()) return getPagecount();
		return pagenum;
	}

	public void setPagenum(Long pagenum) {
		this.pagenum = pagenum;
	}

	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum.longValue();
	}
	
	/**
	 * 当期记录条数
	 * @return
	 */
	public Long getRowcount() {
		return rowcount;
	}

	public void setRowcount(Long rowcount) {
		this.rowcount = rowcount;
	}
	
	/**
	 * 获得分页数
	 * @return 当前分页数
	 */
	public long getPagecount() {
		pagecount = (rowcount + pagerow - 1) / pagerow;
		if(pagecount <=0)pagecount=1;
		return pagecount;
	}
	
}
